﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data;

using System.Threading;
using System.ServiceModel.Activation;
using Jzy.BLL.Mail;
using Jzy.BLL.Calendar;


using Jzy.Model.ESecretary;
using Jzy.BLL.ESecretary;
using Frame.Lib.Core.User;
using Frame.Lib.Common;
using Frame.Lib.Core.Tree;
using Jzy.BLL.OnlineUser;
using Jzy.Model.OnlineUser;


namespace Jzy.Wcf.Secretary
{
   // [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]//多主机头IIS部署需要
    public class SecretarySvc:ISecretarySvc
    {
        private UserBO _user;
        private OnlineUserBO _online;
        public SecretarySvc()
        {
            _user = new UserBO();
            _online = new OnlineUserBO();
        }
        #region ISecretarySvc 成员

        public List<DeptUserInfo> GetDeptUsers()
        {
            List<TreeInfo> dt = _user.GetDeptUserList();
            List<DeptUserInfo> users = new List<DeptUserInfo>();
            foreach (TreeInfo dr in dt)
            {
                DeptUserInfo info = new DeptUserInfo();
                info.ID = dr.ID;
                info.Name =  dr.Name;
                info.UpperID = dr.UpperID;
                users.Add(info);
            }
            return users;
        }

        /// <summary>
        /// 获得当前在线的用户ID,同时刷新自己的最后登录时间
        /// </summary>
        /// <returns></returns>
        public List<int> GetOnlineUserIDs(int userID, string ip)
        {
            //刷新一下自己的登录时间


           // List<OnlineUserInfo> list = online.GetOnlineUserList(DateTime.Now.AddMinutes(-5));

            OnlineUserInfo info = _online.GetInfo(userID);
            info.LastVisitTime = DateTime.Now;
            info.IP = ip;

            _online.Save(info, false);

            List<int> ids = _online.GetOnlineUserIDs(DateTime.Now.AddMinutes(-5));

            return ids;
        }


        public LoginResult CheckLogin(string strLoginName, string strPassword, string ip)
        {
            LoginResult result = _user.CheckLogin(strLoginName, strPassword);
            if (result == LoginResult.Success)
            {
                UserInfo info = _user.GetInfoByLoginName(strLoginName);

                OnlineUserBO online = new OnlineUserBO();
                OnlineUserInfo onlineInfo = online.GetInfo(info.ID);
                if (onlineInfo != null)
                {
                    onlineInfo.LastVisitTime = DateTime.Now;
                    onlineInfo.IP = ip;
                    online.Save(onlineInfo, false);
                }
                else
                {
                    onlineInfo = new OnlineUserInfo() { ID = info.ID, IP = ip, LastVisitTime = DateTime.Now, DeptName = info.DeptName, Name = info.Name };
                    online.Save(onlineInfo, true);
                }
                
            }
            return result;
        }


        public int GetUserID(string strIP)
        {
            return _online.GetUserID(strIP);
        }

       
        public string GetUserIP(int userID)
        {
            return _online.GetUserIP(userID);           
        }


        public string DoWork(string str)
        {
            Thread.Sleep(5000);
            return str;
        }

     
   

        public string GetOAUrl()
        {
            return AppSetting.OAUrl;
        }

        #endregion

        #region ISecretarySvc 成员


        public List<OnlineUserInfo> GetOnlineUserList()
        {
            return _online.GetOnlineUserList(DateTime.Now.AddMinutes(-5));

        }

     

        public OnlineUserInfo GetUserInfo(string loginName)
        {
            UserBO user = new UserBO();
            UserInfo info = user.GetInfoByLoginName(loginName);

            return new OnlineUserInfo() { ID = info.ID, Name = info.Name };
        }

        #endregion

        #region ISecretarySvc 成员


        public RemindMessage GetRemindMessage(int userID)
        {
            MailBO mail = new MailBO(userID);

            RemindMessage msg = new RemindMessage();

            msg.UnreadMailCount = mail.GetUnreadMailCount();

            CalendarBO calendar = new CalendarBO();

            msg.TodayCalendarCount = calendar.GetTodayCalendarCount(userID);
            return msg;
        }

        #endregion

        #region ISecretarySvc 成员


        public List<UserChatInfo> ReceiveMessage(int userID)
        {
            UserChatBO chat = new UserChatBO();
            List<UserChatInfo> list = chat.GetList(userID);
            return list;
        }

        public void SendMessage(UserChatInfo info)
        {
            UserChatBO chat = new UserChatBO();
            info.ID = Guid.NewGuid();
            chat.Save(info,true);
        }

        #endregion

        #region ISecretarySvc 成员


        public void SendBatchMessage(UserChatInfo info, IEnumerable<int> receiveIDs)
        {
            UserChatBO chat = new UserChatBO();
            foreach(int id  in receiveIDs)
            {
                info.ID= Guid.NewGuid();
                info.ReceiveID= id;
                chat.Save(info,true);
            }
        }

        #endregion
    }
}
